//
//  HSParameters.m
//  HSMediator
//
//  Created by fan on 2021/10/15.
//

#import "HSParameters.h"

#define kDefaultCallBack @"kDefaultCallBack"
#define kValueCallBack @"kValueCallBack"

@interface HSParameters ()

@property (nonatomic, strong) NSMutableDictionary *mutableDic;

@property (nonatomic, strong) NSString *currentKey;

@end

@implementation HSParameters

+ (HSParameters * _Nonnull (^)(NSDictionary * _Nonnull))parameters{
    return ^HSParameters *(NSDictionary *parameters) {
        HSParameters *act = [HSParameters create];
        act.mutableDic = [parameters mutableCopy];
        return act;
    };;
}

+ (HSParameters *)create{
    return [self new];
}

- (HSParameters * _Nonnull (^)(NSString * _Nonnull))key{
    return ^HSParameters *(NSString *key) {
        if (key && key.length > 0) {
            self.currentKey = key;
        }
        return self;
    };
}

- (HSParameters * _Nonnull (^)(id _Nonnull))value{
    return ^HSParameters *(id value) {
        if (self.currentKey && self.currentKey.length > 0) {
            self.mutableDic[self.currentKey] = value;
            //设置完把当前key置空，防止重复设置
            self.currentKey = nil;
        }
        return self;
    };
}

- (id  _Nonnull (^)(NSString *))valueForKey{
    return ^id (NSString *key) {
        if (key && key.length) {
            return self.mutableDic[key];
        }else {
            return nil;
        }
    };
}

- (NSDictionary *)params{
    return [self.mutableDic copy];
}

- (NSMutableDictionary *)mutableDic{
    if (!_mutableDic) {
        _mutableDic = [NSMutableDictionary dictionary];
    }
    return _mutableDic;
}

- (HSDefaultCallBack)defaultCallBack{
    return self.mutableDic[kDefaultCallBack];
}

- (void)setDefaultCallBack:(HSDefaultCallBack)defaultCallBack{
    self.mutableDic[kDefaultCallBack] = defaultCallBack;
}

- (HSValueCallBack)valueCallBack{
    return self.mutableDic[kValueCallBack];
}

- (void)setValueCallBack:(HSValueCallBack)valueCallBack{
    self.mutableDic[kValueCallBack] = valueCallBack;
}

- (HSParameters * _Nonnull (^)(HSDefaultCallBack _Nonnull))defaultCB{
    return ^HSParameters *(HSDefaultCallBack defaultCB){
        self.defaultCallBack = defaultCB;
        return self;
    };
}

- (HSParameters * _Nonnull (^)(HSValueCallBack _Nonnull))valueCB{
    return ^HSParameters *(HSValueCallBack valueCB){
        self.valueCallBack = valueCB;
        return self;
    };
}

@end
